package com.bdqn.thread;

import com.bdqn.component.ArticleStatus;
import com.bdqn.component.SensitivewordFilter;
import com.bdqn.dao.ArticleCheckMapper;
import com.bdqn.dao.ArticleMapper;
import com.bdqn.entity.ArticleCheck;
import com.bdqn.entity.ArticleWithBLOBs;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;
import java.util.Set;
import java.util.UUID;

public class SensitivewordFilterThead extends Thread {
    //敏感词过滤组件
    private SensitivewordFilter sensitivewordFilter;
    //被过滤的文章对象
    private ArticleWithBLOBs article;
    //原本想设置文章的状态
    private Integer originStatus;
    //审核结果的mapper
    private ArticleCheckMapper articleCheckMapper;
    //文章的mapper
    private ArticleMapper articleMapper;
    //事务管理组件
    private DataSourceTransactionManager transactionManager;

    @Override
    public void run() {
        try {
            sensitivewordFilter.reloadInitKeyWord();
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程开始----");
        //创建核实结果对象
        ArticleCheck ac = new ArticleCheck();
        ac.setUuid(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        ac.setArticleUuid(article.getUuid());
        ac.setChecktime(new Date());
        ac.setEvent("无");
        System.out.println("ac打印第一" + ac);
        //过滤标题
        Set<String> result = null;
        if (StringUtils.isBlank(article.getTitle())) {
            result = sensitivewordFilter.getSensitiveWord(article.getTitle(), 2);
        }
        //过滤内容
        Set<String> result1 = sensitivewordFilter.getSensitiveWord(article.getContent(), 2);
        //摘要
        Set<String> result2 = null;
        if (StringUtils.isBlank(article.getSummary())) {
        result2 = sensitivewordFilter.getSensitiveWord(article.getSummary(), 2);
        }
        //获得事务对象
        DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition();
        transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);


        try {
            //创建新的对象
            ArticleWithBLOBs newArticle = new ArticleWithBLOBs();
            newArticle.setUuid(article.getUuid());

            int count = 0;
            //未通过
            if ((result != null && result.size() > 0) || (result1 != null && result1.size() > 0) || (result2 != null && result2.size() > 0)) {
                newArticle.setStatus(ArticleStatus.NOT_PASS.getValue());
                count = articleMapper.updateByPrimaryKeySelective(newArticle);
                if (count > 0) {
                    StringBuffer sb = new StringBuffer("");
                    sb.append("【标题】包含以下违法字[");
                    if ((result != null && result.size() > 0)) {
                        for (String str : result) {
                            sb.append(str + "、");
                        }
                        sb.append("]");
                    }
                    if (result1 != null && result1.size() > 0) {
                        sb.append("【内容】包含以下违法字[");
                        for (String str : result1) {
                            sb.append(str + "、");
                        }
                        sb.append("]");

                    }
                    if (result2 != null && result2.size() > 0) {
                        sb.append("【摘要】包含以下违法字[");
                        for (String str : result2) {
                            sb.append(str + "、");
                        }
                        sb.append("]");

                    }
                    System.out.println("审核没有过");
                    ac.setEvent(sb.toString());
                    ac.setStatus(ArticleStatus.NOT_PASS.getName());
                }
            } else {
                System.out.println("审核通过");
                newArticle.setStatus(originStatus);
                ac.setStatus(ArticleStatus.PASS.getName());
                count = articleMapper.updateByPrimaryKeySelective(newArticle);
            }
            if (count > 0) {
                System.out.println(ac);
                if (articleCheckMapper.insertSelective(ac) > 0) {
                    //提事务
                    transactionManager.commit(transStatus);
                    return;
                }
            }
            transactionManager.rollback(transStatus);
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(transStatus);
        }

    }


    public SensitivewordFilterThead(SensitivewordFilter sensitivewordFilter, ArticleWithBLOBs article, Integer originStatus, ArticleCheckMapper articleCheckMapper,
                                    ArticleMapper articleMapper, DataSourceTransactionManager transactionManager) {
        this.sensitivewordFilter = sensitivewordFilter;
        this.article = article;
        this.originStatus = originStatus;
        this.articleCheckMapper = articleCheckMapper;
        this.articleMapper = articleMapper;
        this.transactionManager = transactionManager;
    }
}
